package com.example.scene;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.IEntityFactory;
import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.particle.ParticleSystem;
import org.andengine.entity.particle.SpriteParticleSystem;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.emitter.RectangleParticleEmitter;
import org.andengine.entity.particle.initializer.VelocityParticleInitializer;
import org.andengine.entity.particle.modifier.AlphaParticleModifier;
import org.andengine.entity.particle.modifier.RotationParticleModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.CameraScene;
import org.andengine.entity.scene.IOnAreaTouchListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.progress.ProgressCallable;

import android.content.SharedPreferences;
import android.hardware.SensorManager;
import android.os.CountDownTimer;
import android.text.style.BulletSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.example.base.BaseScene;
import com.example.fruit.R;
import com.example.fruitpool.Boom;
import com.example.fruitpool.BoomPool;
import com.example.fruitpool.Fruit;
import com.example.fruitpool.FruitPool;
import com.example.input.Input.TouchEvent;
import com.example.manager.LevelManager;
import com.example.manager.SceneManager;
import com.example.manager.SceneManager.SceneType;
import com.example.scene.GameScene.PauseableTimerHandler;
import com.example.touch.MultiTouchHandler;

public class GameScene extends BaseScene implements IOnMenuItemClickListener {
	public Text text;

	public MultiTouchHandler touch;
	final RectangleParticleEmitter particleEmitter = null;
	final SpriteParticleSystem particleSystem = null;
	public int mIndex = 0;
	public static ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
	public static ArrayList<Boom> boomList = new ArrayList<Boom>();

	public int fruitCount = 0, boomCount = 0;
	ArrayList<Runnable> arrFruit = new ArrayList<Runnable>();
	ArrayList<Runnable> arrBoom = new ArrayList<Runnable>();

	static int delayFruit = 4000;
	public static String score;

	int missScore = 0;
	int missBoom = 0;
	int currentLevel = 1;
	int numMissScore = 3000;
	float startTime = System.currentTimeMillis();
	final ArrayList<Thread> A = new ArrayList<Thread>();
	private SharedPreferences audioOptions;
	private SharedPreferences.Editor audioEditor;
	public static boolean isPlay;
	private CameraScene mPauseScene;
	private boolean runningFlag = false;
	private boolean pauseFlag = false;
	boolean okFinalLevel = false;
	
	public GameScene() {
		runningFlag = true;
		this.registerUpdateHandler(defect);
		score = new String("0");
	}

	public void setLevel(int i) {
		currentLevel = i;
	}

	public Scene getGameScene() {
		return this;
	}

	public void removeSprite(final Fruit _sprite, Iterator it) {
		activity.runOnUpdateThread(new Runnable() {

			@Override
			public void run() {
				getGameScene().detachChild(_sprite.sprit);
				_sprite.sprit.clearUpdateHandlers();
			}
		});
		it.remove();
	}

	public void removeBoom(final Boom _sprite, Iterator it) {
		activity.runOnUpdateThread(new Runnable() {

			@Override
			public void run() {
				getGameScene().detachChild(_sprite.sprit);
				_sprite.sprit.clearUpdateHandlers();
			}
		});
		it.remove();
	}

	public void pauseTimerHandler() {
		timer.mPause = true;
		timer1.mPause = true;
		timer2.mPause = true;
		timer3.mPause = true;
		timer4.mPause = true;
		timer5.mPause = true;
		timer6.mPause = true;
		timer7.mPause = true;
		timer8.mPause = true;
		timer9.mPause = true;
	}

	IUpdateHandler defect = new IUpdateHandler() {

		@Override
		public void reset() {

		}

		@Override
		public void onUpdate(float pSecondsElapsed) {
			try {
				synchronized (this) {
					// Log.d("update12", "update12");
					if (missScore > numMissScore) {
						pauseTimerHandler();
						resetGame("crash");
						fruitList.clear();
						boomList.clear();
						clearUpdateHandlers();
						getGameScene().setChildScene(cam, false, true, true);
						engine.vibrate(1000);
						slashBackGround();
						// pauseTimerHandler();
						// SceneManager.getInstance().loadResultScene(engine);

					}
					// Log.d("update", "update");
					Iterator<Fruit> targets = fruitList.iterator();

					Fruit _target;
					// Log.d("update1", "update1");
					// iterating over the targets
					while (targets.hasNext()) {
						_target = targets.next();
						// if target passed the left edge of the screen, then
						// remove it
						// and call a fail
						if (_target.sprit.getY() >= (activity.CAMERA_HEIGHT + 55)) {
							// removeSprite(_target, targets);
							// Log.d("update:", _target.sprit.getX()+"");
							int x, y;
							if(!MultiTouchHandler.isTouchIgnore){	
								posX++;
								if (posX == 1) {
								x = (int) x1.getX();
								y = (int) x1.getX();
								ringX(x1, x1.getX(), x1.getY());
								// x1.setPosition(x, y);
								Log.d("pos", x1.getX() + ":" + x1.getY());
							} else if (posX == 2) {
								ringX(x2, x2.getX(), x2.getY());
								Log.d("pos", x2.getX() + ":" + x2.getY());
							} else if (posX == 3) {
								ringX(x3, x3.getX(), x3.getY());
								Log.d("pos", x3.getX() + ":" + x3.getY());
							}
							
							missScore++;
							}
							FruitPool.sharedFruitPool().onHandleRecycleItem(
									_target);
							// targets.remove();
							removeSprite(_target, targets);
							Log.d("missScore", missScore + "");
						}
					}
					Iterator<Boom> booms = boomList.iterator();
					Boom boom;
					// Log.d("update1", "update1");
					// iterating over the targets
					while (booms.hasNext()) {
						boom = booms.next();

						// if target passed the left edge of the screen, then
						// remove it
						// and call a fail
						if (boom.sprit.getY() >= (activity.CAMERA_HEIGHT + 55)) {
							missBoom++;
							BoomPool.sharedBoomPool().onHandleRecycleItem(boom);
							// booms.remove();
							removeBoom(boom, booms);
							// fail();
							// break;
						}
					}
					if(LevelManager.endingLevel==true && fruitList.size()==0 && boomList.size()==0){
						Log.d("pos", "finish");
						//LevelManager.getInstance().goToNextLevel();
						RingLeaderLevel.getSharedInstance().goLevelNext();
						LevelManager.endingLevel=false;
					}
					if(okFinalLevel==true && fruitList.size()==0 && boomList.size()==0){
						Log.d("pos", "finish");
						//pauseFinalLevel();
						RingLeaderLevel.getSharedInstance().goLevel2("fail");
						okFinalLevel = false;
					}
					/*if(RingLeaderLevel.miss>=2){
						RingLeaderLevel.getSharedInstance().goLevel2("fail");
					}
					if(RingLeaderLevel.str.length()==RingLeaderLevel.length){
						RingLeaderLevel.getSharedInstance().goLevel2("pass");
					}*/
					Log.d("sizelist",
							fruitList.size() + ",boom:" + boomList.size());
				}

			} catch (Exception e) { 
				// TODO: handle exception
			}
		}
	};
	public int posX = 0;

	private void ringX(final TiledSprite x1, final float x, final float y) {
		ScaleModifier sMod = new ScaleModifier(0.2f, 1f, 1.6f);
		ScaleModifier sMod1 = new ScaleModifier(0.2f, 1.0f, 0.5f);
		ScaleModifier sMod2 = new ScaleModifier(0.2f, 0.5f, 1f);
		ScaleModifier sMod3 = new ScaleModifier(0.2f, 1f, 1.5f);
		ScaleModifier sMod4 = new ScaleModifier(0.2f, 1.5f, 1.0f);
		ScaleModifier sMod5 = new ScaleModifier(0.2f, 1.6f, 1f);
		float x11 = x;
		float y11 = y;
		MoveModifier mMod = new MoveModifier(0.1f, x11, x11 + 4, y11, y11 + 4);
		MoveModifier mMod1 = new MoveModifier(0.1f, x11, x11 - 4, y11, y11 + 4);
		MoveModifier mMod2 = new MoveModifier(0.1f, x11, x11 + 4, y11, y11 - 4);
		MoveModifier mMod3 = new MoveModifier(0.1f, x11, x11 - 4, y11, y11 - 4);
		MoveModifier mMod4 = new MoveModifier(0.1f, x11, x11 - 3, y11, y11 + 1);
		MoveModifier mMod5 = new MoveModifier(0.1f, x11, x11 + 1, y11, y11 - 4);
		MoveModifier mMod6 = new MoveModifier(0.1f, x11, x11 - 3, y11, y11 - 5);
		MoveModifier mMod7 = new MoveModifier(0.1f, x11, x11 + 1, y11, y11 + 4);

		// MoveModifier mMod4 = new MoveModifier(0.1f, x1.getX(), y, x1.getY(),
		// y);
		x1.registerEntityModifier(new ParallelEntityModifier(
				new SequenceEntityModifier(mMod, mMod1, mMod2, mMod3, mMod4,
						mMod5, mMod6, mMod7), new SequenceEntityModifier(sMod,
						sMod1, sMod2, sMod3, sMod4)));
		registerUpdateHandler(new TimerHandler(1.0f, new ITimerCallback() {

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				x1.setPosition(x, y);
				x1.setCurrentTileIndex(1);
			}
		}));
		// x1.registerEntityModifier(new
		// ParallelEntityModifier(mMod,mMod1,mMod2,mMod3,mMod4,sMod,sMod1,sMod2));
	}

	final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(0f, 0f,
			0f);
	private float[] up = { -SensorManager.GRAVITY_EARTH * 3, -7 * 3f, -5 * 3,
			-3 * 2, -2 * 2, -1 };
	private float[] xRight = { 2f, 2.5f, 3f, 3.5f, 4f, 4.5f };
	private float[] xLeft = { -2f, -2.5f, -3f, -3.5f, -4f, -4.5f };

	private void addFace(final float pX, final float pY, final float pY1,
			final PhysicsWorld mPhysicsWorld, final IsRun isRun,
			final Sprite face, final int side, final float moveX) {
		synchronized (mPhysicsWorld) {
			final float f = (pY - pY1) / 6;

			final Body body = PhysicsFactory.createBoxBody(mPhysicsWorld, face,
					BodyType.DynamicBody, objectFixtureDef);

			mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
					body, true, true) {

				@Override
				public void onUpdate(float pSecondsElapsed) {
					synchronized (mPhysicsWorld) {
						super.onUpdate(pSecondsElapsed);
						if (side == 0) {// / left->right
							if (face.getY() > pY1) {
								if (isRun.isRunUp) {
									/*
									 * if (face.getY() > (pY1 + 5 * f) &&
									 * face.getY() < (pY1 + 6 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xRight[0], up[0])); if (face.getY() >
									 * (pY1 + 4 * f) && face.getY() < (pY1 + 5 *
									 * f)) mPhysicsWorld.setGravity(new Vector2(
									 * xRight[1], up[1])); if (face.getY() >
									 * (pY1 + 3 * f) && face.getY() < (pY1 + 4 *
									 * f)) mPhysicsWorld.setGravity(new Vector2(
									 * xRight[2], up[2])); if (face.getY() >
									 * (pY1 + 2 * f) && face.getY() < (pY1 + 3 *
									 * f)) mPhysicsWorld.setGravity(new Vector2(
									 * xRight[3], up[3])); if (face.getY() >
									 * (pY1 + 1 * f) && face.getY() < (pY1 + 2 *
									 * f)) mPhysicsWorld.setGravity(new Vector2(
									 * xRight[4], up[4])); if (face.getY() > pY1
									 * && face.getY() < (pY1 + 1 * f))
									 */
									mPhysicsWorld.setGravity(new Vector2(moveX,
											-SensorManager.GRAVITY_EARTH));
								} else {
									mPhysicsWorld.setGravity(new Vector2(moveX,
											SensorManager.GRAVITY_EARTH));
								}
							}
						} else {
							if (face.getY() > pY1) {
								if (isRun.isRunUp) {
									/*
									 * if (face.getY() > (pY1 + 5 * f) &&
									 * face.getY() < (pY1 + 6 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xLeft[0], up[0])); if (face.getY() > (pY1
									 * + 4 * f) && face.getY() < (pY1 + 5 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xLeft[1], up[1])); if (face.getY() > (pY1
									 * + 3 * f) && face.getY() < (pY1 + 4 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xLeft[2], up[2])); if (face.getY() > (pY1
									 * + 2 * f) && face.getY() < (pY1 + 3 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xLeft[3], up[3])); if (face.getY() > (pY1
									 * + 1 * f) && face.getY() < (pY1 + 2 * f))
									 * mPhysicsWorld.setGravity(new Vector2(
									 * xLeft[4], up[4])); if (face.getY() > pY1
									 * && face.getY() < (pY1 + 1 * f))
									 */
									mPhysicsWorld.setGravity(new Vector2(
											-moveX,
											-SensorManager.GRAVITY_EARTH));
								} else {
									mPhysicsWorld
											.setGravity(new Vector2(-moveX,
													SensorManager.GRAVITY_EARTH));
								}
							}

						}
						if (face.getY() < pY1) {
							isRun.isRunUp = false;
							mPhysicsWorld.setGravity(new Vector2(0,
									SensorManager.GRAVITY_EARTH * 5));
						}
					}
				}
			});
		}
		// face.getX();
	}

	public boolean onAreaTouched(
			final org.andengine.input.touch.TouchEvent pSceneTouchEvent,
			final ITouchArea pTouchArea, final float pTouchAreaLocalX,
			final float pTouchAreaLocalY) {
		if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
			Log.d("pos", "pause");
		}
		return b;
	}

	/*
	 * Sprite pausedSprite; Sprite playSprite; Sprite restartSprite; Sprite
	 * quitSprite; Sprite titlePauseChildScene;
	 */
	public void slashBackGround() {
		setTextChange(backGroundSprite);

	}

	CameraScene cam;

	@Override
	public void createScene() {
		// this.registerUpdateHandler(defect);
		audioOptions = activity.getSharedPreferences("audio",
				activity.MODE_PRIVATE);
		audioEditor = audioOptions.edit();
		mPauseScene = new CameraScene(camera);
		cam = new CameraScene(camera);

		Sprite back = new Sprite(0, 0, activity.CAMERA_WIDTH + 1,
				activity.CAMERA_HEIGHT + 1,
				resourcesManager.game_background_region, vbom);
		back.setAlpha(0.5f);
		Sprite backGround = new Sprite(0, 0, activity.CAMERA_WIDTH + 1,
				activity.CAMERA_HEIGHT + 1,
				resourcesManager.game_background_region, vbom);
		backGround.setAlpha(0.5f);

		mPauseScene.attachChild(backGround);

		// cam
		cam.attachChild(back);
		Text result = new Text(0, 0, resourcesManager.font, "Game Over", vbom);
		final int x1 = (int) (activity.CAMERA_WIDTH / 2 - result.getWidth() / 2);
		final int y1 = (int) (activity.CAMERA_HEIGHT / 2 - result.getHeight() / 2);
		result.setPosition(x1,
				activity.mCamera.getHeight() + result.getHeight());
		MoveYModifier mod = new MoveYModifier(2, result.getY(), y1) {
			@Override
			protected void onModifierFinished(IEntity pItem) {
				getGameScene().clearChildScene();
				SceneManager.getInstance().loadResultScene(engine);
			}
		};
		result.registerEntityModifier(mod);
		cam.attachChild(result);
		cam.setBackgroundEnabled(false);

		/* Make the label centered on the camera. */
		final float x = (int) (camera.getWidth() / 2 - resourcesManager.mTiledTextureRegionPlay
				.getWidth() / 2);
		final float y = (int) (camera.getHeight() / 2 - resourcesManager.mTiledTextureRegionPlay
				.getHeight() / 2);
		final float xQuit = (camera.getWidth() - resourcesManager.quitgame_region
				.getWidth());
		final float yQuit = (camera.getHeight() - resourcesManager.quitgame_region
				.getHeight());

		TiledSprite pausedSprite = new TiledSprite((float)x-50, (float)y,90,90,
				resourcesManager.mTiledTextureRegionPlay, vbom) {
			@Override
			public boolean onAreaTouched(
					org.andengine.input.touch.TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				getGameScene().clearChildScene();
				Log.d("pos", "pasdas");
				timer.mPause = false;
				timer1.mPause = false;
				timer2.mPause = false;
				timer3.mPause = false;
				timer4.mPause = false;
				timer5.mPause = false;
				timer6.mPause = false;
				timer7.mPause = false;
				timer8.mPause = false;
				timer9.mPause = false;
				restartInfor();
				this.setCurrentTileIndex(1);
				en=0;
				// backGroundSprite.setAlpha(1f);
				// engine.start();
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
			}
		};
		pausedSprite.setCurrentTileIndex(0);
		TiledSprite restartSprite = new TiledSprite(x+50, y,90,90,
				resourcesManager.mTiledTextureRegionResume, vbom) {
			@Override
			public boolean onAreaTouched(
					org.andengine.input.touch.TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				resetGame("pause");
				getGameScene().clearChildScene();
				Log.d("pos", "pasdas");
				restartInfor();
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
			}
		};
		Sprite quitSprite = new Sprite(xQuit, yQuit,
				resourcesManager.quitgame_region, vbom) {
			@Override
			public boolean onAreaTouched(
					org.andengine.input.touch.TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// getGameScene().clearChildScene();
				// Log.d("pos", "pasdas");
				resetGame("quit");
				restartInfor();
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
						pTouchAreaLocalY);
			}

		};
		Sprite titleGame = new Sprite(camera.getWidth() / 2
				- resourcesManager.titlepausegame_region.getWidth() / 2,
				y - 100, resourcesManager.titlepausegame_region, vbom);
		mPauseScene.registerTouchArea(pausedSprite);
		mPauseScene.registerTouchArea(restartSprite);
		mPauseScene.registerTouchArea(quitSprite);

		mPauseScene.setTouchAreaBindingOnActionDownEnabled(true);
		mPauseScene.setTouchAreaBindingOnActionMoveEnabled(true);
		mPauseScene.attachChild(pausedSprite);
		mPauseScene.attachChild(restartSprite);
		mPauseScene.attachChild(quitSprite);
		mPauseScene.attachChild(titleGame);
		// makes the scene transparent
		mPauseScene.setBackgroundEnabled(false);

		float startTime = System.currentTimeMillis();
		Log.d("mPhysicsWorld", "mPhysicsWorld");
		FruitPool.prepareManager(resourcesManager, vbom);
		BoomPool.prepareManager(resourcesManager, vbom);
		createBackground();
		createScore();
		// createBoom();
		createFruit();
		createSoundMusicX();
		touch = new MultiTouchHandler(resourcesManager, vbom, this);
		// this.registerUpdateHandler(mPhysicsWorld);
		// registerUpdateHandler(defect);
		// Log.d("score","createScene");

		// create HUD
		/*
		 * HUD hud = new HUD(); activity.mCamera.setHUD(hud);
		 */
	}

	private void restartInfor() {
		MultiTouchHandler.isGiftBox = false;
		MultiTouchHandler.isTouchIgnore = false;		
	}
	public void resetGame(String string) {
		score = "0";
		posX = 0;
		timer.reset();
		timer1.reset();
		timer2.reset();
		timer3.reset();
		timer4.reset();
		timer5.reset();
		timer6.reset();
		timer7.reset();
		timer8.reset();
		timer9.reset();
		timer = null;
		timer1 = null;
		timer2 = null;
		timer3 = null;
		timer4 = null;
		timer5 = null;
		timer6 = null;
		timer7 = null;
		timer8 = null;
		timer9 = null;
		this.clearUpdateHandlers();
		engine.unregisterUpdateHandler(timer);
		engine.unregisterUpdateHandler(timer1);
		engine.unregisterUpdateHandler(timer2);
		engine.unregisterUpdateHandler(timer3);
		engine.unregisterUpdateHandler(timer4);
		engine.unregisterUpdateHandler(timer5);
		engine.unregisterUpdateHandler(timer6);
		engine.unregisterUpdateHandler(timer7);
		engine.unregisterUpdateHandler(timer8);
		engine.unregisterUpdateHandler(timer9);
		// RunLevelById(1);
		if (string.equalsIgnoreCase("quit")) {
			SceneManager.getInstance().loadMenuScene(engine);
		}
		if (string.equalsIgnoreCase("pause"))
			SceneManager.getInstance().loadGameScene(engine);
	}

	boolean b = false;

	private MenuScene pauseScene() {
		b = true;
		final MenuScene pauseGame = new MenuScene(camera);

		final SpriteMenuItem btnPlay = new SpriteMenuItem(3,
				resourcesManager.playgame_region, vbom);
		btnPlay.setPosition(400, 400);
		btnPlay.setScale(2);
		pauseGame.addMenuItem(btnPlay);

		pauseGame.setBackgroundEnabled(false);
		pauseGame.setOnMenuItemClickListener(this);

		return pauseGame;
	}

	TimerHandler boomTimerHandler;

	private void createBoom() {
		/*
		 * activity.mHandler.postDelayed(new Runnable() {
		 * 
		 * @Override public void run() { Random r = new Random(); int count =
		 * r.nextInt(3);
		 * 
		 * for (int i = 0; i < count; i++) { arrBoom.add(mStartBoom); }
		 * 
		 * for (int i = 0; i < count; i++) {
		 * activity.mHandler.postDelayed(arrBoom.get(i), 500); }
		 * activity.mHandler.postDelayed(this, 5000); } }, 3000);
		 */

		/*
		 * float mEffectSpawnDelay = 3f;
		 * 
		 * boomTimerHandler = new TimerHandler(mEffectSpawnDelay, true, new
		 * ITimerCallback() {
		 * 
		 * @Override public void onTimePassed(TimerHandler pTimerHandler) {
		 * Random r = new Random(); int count = r.nextInt(2)+1; for (int i = 0;
		 * i < count; i++) { addBoom(); } } });
		 * this.registerUpdateHandler(boomTimerHandler);
		 */

	}

	/*
	 * Runnable run = new Runnable() {
	 * 
	 * @Override public void run() { Random r = new Random(); arrFruit = new
	 * ArrayList<Runnable>(); int count = r.nextInt(5); for (int i = 0; i < 1;
	 * i++) { arrFruit.add(mStartFruit); }
	 * 
	 * for (int i = 0; i < 1; i++) { activity.mHandler.post(arrFruit.get(i)); }
	 * } };
	 */
	final ArrayList<Runnable> B = new ArrayList<Runnable>();
	TimerHandler textTimerHandler;

	private void createFruit() {
		/*
		 * activity.mHandler.postDelayed(new Runnable() {
		 * 
		 * @Override public void run() { //activity.mHandler.postDelayed(this,
		 * 3000); } }, 500);
		 */
		/*
		 * activity.mHandler.postDelayed(new Runnable() {
		 * 
		 * @Override public void run() { Random r = new Random(); //arrFruit =
		 * new ArrayList<Runnable>(); int count = r.nextInt(5); for (int i = 0;
		 * i < count; i++) { arrFruit.add(mStartFruit); B.add(this); }
		 * 
		 * for (int i = count; i > 0; i--) {
		 * activity.mHandler.postDelayed(arrFruit.get(arrFruit.size()-i),500); }
		 * activity.mHandler.postDelayed(this, 1000); } }, 500);
		 */

		/*
		 * Random r=new Random(); float mEffectSpawnDelay = 2; textTimerHandler
		 * = new TimerHandler(mEffectSpawnDelay, true, new ITimerCallback() {
		 * 
		 * @Override public void onTimePassed(TimerHandler pTimerHandler) {
		 * Log.d("addText","addTextcreate"); Random r = new Random(); int count
		 * = r.nextInt(5)+2; for (int i = 0; i < count; i++) { addText(); } }
		 * 
		 * });
		 * 
		 * this.registerUpdateHandler(textTimerHandler);
		 */
		/*
		 * MyCount count = new MyCount(30000, 1000); count.start();
		 */
		RunLevelById(1);

	}

	// Timer
	// timer,timer1,timer2,timer3,timer4,timer5,timer6,timer7,timer8,timer9;
	/*
	 * public void destroyTimer() { timer.cancel(); timer1.cancel();
	 * timer2.cancel(); timer3.cancel(); timer4.cancel(); timer5.cancel();
	 * timer6.cancel(); timer7.cancel(); timer8.cancel(); timer9.cancel(); }
	 */
	public static boolean isContinuos = false;
	public static boolean isTwoSide = false;
	public static boolean isRunning = true;

	// TimerHandler
	// timer,timer1,timer2,timer3,timer4,timer5,timer6,timer7,timer8,timer9;
	PauseableTimerHandler timer, timer1, timer2, timer3, timer4, timer5,
			timer6, timer7, timer8, timer9;
	float delay = (float) 1.0; // delay for 5 sec.
	float period = (float) 2.0; // repeat every sec.
	float delay1 = (float) 6.0; // delay for 5 sec.
	float period1 = (float) 2.0; // repeat every sec.
	float delay2 = (float) 11.4; // delay for 5 sec.
	float period2 = (float) 1.0; // repeat every sec.
	float delay3 = (float) 14.5; // delay for 5 sec.
	float period3 = (float) 1.0; // repeat every sec.
	float delay4 = (float) 21.0; // delay for 5 sec.
	float period4 = (float) 2.0; // repeat every sec.
	float delay5 = 26f; // delay for 5 sec.
	float period5 = 2f; // repeat every sec.
	float delay6 = 34f; // delay for 5 sec.
	float period6 = 0.8f; // repeat every sec.
	float delay7 = 43f; // delay for 5 sec.
	float period7 = 1f; // repeat every sec.
	float delay8 = 46f; // delay for 5 sec.
	float period8 = 1f; // repeat every sec.
	float delay9 = 51f; // delay for 5 sec.
	float period9 = 1f; // repeat every sec.

	private float[] convertStringToFloat(String[] s) {
		float[] t = new float[s.length];
		for (int i = 0; i < t.length; i++) {
			t[i] = Float.parseFloat(s[i]);
		}
		return t;
	}
	public float[] getDelayPeriodFromResource(int delayName){
		return convertStringToFloat(activity.getResources()
				.getStringArray(delayName));		
	}
	
	private void RunLevelById(int i) {
		/*delay = (float) 1.0; // delay for 5 sec.
		period = (float) 2.0; // repeat every sec.
		delay1 = (float) 6.0; // delay for 5 sec.
		period1 = (float) 2.0; // repeat every sec.
		delay2 = (float) 11.4; // delay for 5 sec.
		period2 = (float) 1.0; // repeat every sec.
		delay3 = (float) 14.5; // delay for 5 sec.
		period3 = (float) 1.0; // repeat every sec.
		delay4 = (float) 21.0; // delay for 5 sec.
		period4 = (float) 2.0; // repeat every sec.
		delay5 = 26f; // delay for 5 sec.
		period5 = 2f; // repeat every sec.
		delay6 = 34f; // delay for 5 sec.
		period6 = 0.8f; // repeat every sec.
		delay7 = 43f; // delay for 5 sec.
		period7 = 1f; // repeat every sec.
		delay8 = 46f; // delay for 5 sec.
		period8 = 1f; // repeat every sec.
		delay9 = 51f; // delay for 5 sec.
		period9 = 1f; // repeat every sec.
*/		Log.d("pos", delay + ";");
		setCounter();
		final int t = new Random().nextInt(4)+4;
		final float[] delays = convertStringToFloat(activity.getResources()
				.getStringArray(R.array.delay1));
		final float[] periods = convertStringToFloat(activity.getResources()
				.getStringArray(R.array.period1));

		timer = new PauseableTimerHandler(delays[0], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer is running");
							isContinuos = false;
							addText();
							// delay += period;
							timer.setTimerSeconds(periods[0]);
							count++;
							if (count == 1) {// 2
								// engine.unregisterUpdateHandler(timer);
								// timer.setTimerCallbackTriggered(true);
						//		LevelManager.endingLevel = true;
								timer.setAutoReset(false);
							}
					//		RingLeaderLevel.getSharedInstance().goLevel1();
						}
					}
				});

		engine.registerUpdateHandler(timer);		
		timer1 = new PauseableTimerHandler(delays[1], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						//if (isStopped)
						{
							Log.d("pos", "timer1 is running");
							isContinuos = false;
							addText();
							addText();
							// delay1 += period1;
							timer1.setTimerSeconds(periods[1]);
							count1++;
							if (count1 == 1) {// 2
								// engine.unregisterUpdateHandler(timer1);
								// timer1.setTimerCallbackTriggered(true);
								Log.d("pos", "endingle");
								//LevelManager.endingLevel = true;
								timer1.setAutoReset(false);
								RingLeaderLevel.getSharedInstance().goLevel1();
							}
						}
					//	timer1.setAutoReset(false);
					}
				});
		engine.registerUpdateHandler(timer1);
		timer2 = new PauseableTimerHandler(delays[2], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer2 is running");
							isContinuos = false;
							addBoom();
							// engine.unregisterUpdateHandler(timer2);
							// timer2.setTimerCallbackTriggered(true);
							timer2.setAutoReset(false);
						}
					}
				});
		engine.registerUpdateHandler(timer2);

		timer3 = new PauseableTimerHandler(delays[3], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer3 is running");
							isContinuos = true;
							addText();
							// delay3 += period3;
							timer3.setTimerSeconds(periods[3]);
							count3++;
							if (count3 == 5) {// 6
								// engine.unregisterUpdateHandler(timer3);
								// timer3.setTimerCallbackTriggered(true);
								timer3.setAutoReset(false);
							}
						}
					}
				});
		engine.registerUpdateHandler(timer3);

		timer4 = new PauseableTimerHandler(delays[4], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer4 is running");
							isContinuos = false;
							addText();
							addBoom();
							// delay4 += period4;
							timer4.setTimerSeconds(periods[4]);
							count4++;
							if (count4 == 1) {// 2
								// engine.unregisterUpdateHandler(timer4);
								// timer4.setTimerCallbackTriggered(true);
								timer4.setAutoReset(false);
							}
							if(t==4&&count4==1){
							//	pausedTimerHandler();
								RingLeaderLevel.getSharedInstance().goLevel1();
							}
						}
					}
				});
		engine.registerUpdateHandler(timer4);

		timer5 = new PauseableTimerHandler(delays[5], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer5 is running");
							isContinuos = false;
							if (count5 == 0) {
								addText();
								addText();
							}
							if (count5 == 1) {
								addText();
								addText();
								addText();

							}
							if (count5 == 2) {
								addText();
								addText();
							}
							// delay5 += period5;
							timer5.setTimerSeconds(periods[5]);
							count5++;
							if (count5 == 2) {// 3
								// engine.unregisterUpdateHandler(timer5);
								// timer5.setTimerCallbackTriggered(true);
								timer5.setAutoReset(false);
							}
							if(t==5 && count5==1){
							//	pausedTimerHandler();
								RingLeaderLevel.getSharedInstance().goLevel1();
							}
						}
					}
				});
		engine.registerUpdateHandler(timer5);
		timer6 = new PauseableTimerHandler(delays[6], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer6 is running");
							isContinuos = true;
							addTwoSideText();
							addTwoSideText();
							timer6.setTimerSeconds(periods[6]);
							count6++;
							if (count6 == 6) {// 7
								// engine.unregisterUpdateHandler(timer6);
								// timer6.setTimerCallbackTriggered(true);
								timer6.setAutoReset(false);
							}
							if(t==6 && count6==1){
							//	pausedTimerHandler();
								RingLeaderLevel.getSharedInstance().goLevel1();
							}
						}
					}
				});
		engine.registerUpdateHandler(timer6);
		timer7 = new PauseableTimerHandler(delays[7], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer7 is running");
							isContinuos = false;
							addText();
							addText();
							addText();
							timer7.setTimerSeconds(periods[7]);
							// engine.unregisterUpdateHandler(timer7);
							// timer7.setTimerCallbackTriggered(true);
							timer7.setAutoReset(false);
						}
						if(t==7){
						//	pausedTimerHandler();
							RingLeaderLevel.getSharedInstance().goLevel1();
						}
					}
				});
		engine.registerUpdateHandler(timer7);
		timer8 = new PauseableTimerHandler(delays[8], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer8 is running");
							isContinuos = false;
							addText();
							addText();
							timer8.setTimerSeconds(periods[8]);
							count8++;
							if (count8 == 1)// 2
							{
								timer8.setAutoReset(false);
								// timer8.setTimerCallbackTriggered(true);
								// engine.unregisterUpdateHandler(timer8);
							}
							if(t==8 && count8==1){
							//	pausedTimerHandler();
								RingLeaderLevel.getSharedInstance().goLevel1();
							}
						}
					}
				});
		engine.registerUpdateHandler(timer8);
		timer9 = new PauseableTimerHandler(delays[9], true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						if (!isStopped) {
							Log.d("pos", "timer9 is running");
							isContinuos = false;
							addText();
							addText();
							addBoom();
							addText();
							timer9.setTimerSeconds(periods[9]);
							count9++;
							if (count9 == 4) {// 5
								// engine.unregisterUpdateHandler(timer9);
								// timer9.setTimerCallbackTriggered(true);
								timer9.setAutoReset(false);
								LevelManager.endingLevel = true;
							}
						}
					}
				});
		engine.registerUpdateHandler(timer9);
		/*
		 * timer8.scheduleAtFixedRate(new TimerTask() {
		 * 
		 * public void run() { if (!isStopped) {
		 * Log.d("pos","timer8 is running"); isContinuos = false; addText();
		 * addText(); count8++; if (count8 == 2) timer8.cancel(); } else {
		 * timer8.cancel(); } } }, delay8, period8);
		 * timer9.scheduleAtFixedRate(new TimerTask() {
		 * 
		 * public void run() { if (!isStopped) {
		 * Log.d("pos","timer9 is running"); isContinuos = false; addText();
		 * addText(); addBoom(); addText(); count9++; if (count9 == 5) {
		 * timer9.cancel(); } } else { timer9.cancel(); } } }, delay9, period9);
		 * }
		 */
	}

	/*public void pausedTimerHandler() {
		timer.mPause = true;
		timer1.mPause = true;
		timer2.mPause = true;
		timer3.mPause = true;
		timer4.mPause = true;
		timer5.mPause = true;
		timer6.mPause = true;
		timer7.mPause = true;
		timer8.mPause = true;
		timer9.mPause = true;
	}*/
	public void resumeTimerHandler(boolean b) {
		if(timer!=null)
		timer.mPause = b;
		if(timer1!=null)timer1.mPause = b;
		if(timer2!=null)timer2.mPause = b;
		if(timer3!=null)timer3.mPause = b;
		if(timer4!=null)timer4.mPause = b;
		if(timer5!=null)timer5.mPause = b;
		if(timer6!=null)timer6.mPause = b;
		if(timer7!=null)timer7.mPause = b;
		if(timer8!=null)timer8.mPause = b;
		if(timer9!=null)timer9.mPause = b;
	}
	
	private void setCounter() {
		count = count1 = count2 = count3 = count4 = count5 = count6 = count7 = count8 = count9 = 0;
	}

	int count = 0;
	int count1 = 0;
	int count2 = 0;
	int count3 = 0;
	int count4 = 0;
	int count5 = 0;

	int count6 = 0;
	int count8 = 0;
	int count7 = 0;
	int count9 = 0;
	public boolean isStopped = false;

	private void createSoundMusicX() {
		if (audioOptions.getBoolean("musicOn", true)) {
			index = 0;
		} else {
			index = 1;
		}
		if (audioOptions.getBoolean("effectsOn", true)) {
			indexS = 0;
		} else {
			indexS = 1;
		}
		music = new TiledSprite(530, 10, 50, 40,
				resourcesManager.mTiledTextureRegionMusic, vbom) {
			@Override
			public boolean onAreaTouched(
					final org.andengine.input.touch.TouchEvent pAreaTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				switch (pAreaTouchEvent.getAction()) {
				case org.andengine.input.touch.TouchEvent.ACTION_DOWN:
					break;
				case org.andengine.input.touch.TouchEvent.ACTION_UP:
					if (audioOptions.getBoolean("musicOn", true)) {
						audioEditor.putBoolean("musicOn", false);
						if (resourcesManager.musicGame.isPlaying())
							resourcesManager.musicGame.pause();
						AlphaModifier mod = new AlphaModifier(0.3f, 1, 0);
						AlphaModifier mod1 = new AlphaModifier(0.01f, 0, 1);
						this.registerEntityModifier(new SequenceEntityModifier(
								mod, mod1));
						index = 1;
						this.registerUpdateHandler(new TimerHandler(0.3f,
								new ITimerCallback() {

									@Override
									public void onTimePassed(
											TimerHandler pTimerHandler) {
										music.setCurrentTileIndex(index);
									}
								}));

					} else {
						audioEditor.putBoolean("musicOn", true);
						if (!resourcesManager.musicGame.isPlaying())
							resourcesManager.musicGame.play();
						else
							resourcesManager.musicGame.resume();

						AlphaModifier mod = new AlphaModifier(0.3f, 1, 0);
						AlphaModifier mod1 = new AlphaModifier(0.0001f, 0, 1);
						this.registerEntityModifier(new SequenceEntityModifier(
								mod, mod1));
						index = 0;

						this.registerUpdateHandler(new TimerHandler(0.3f,
								new ITimerCallback() {

									@Override
									public void onTimePassed(
											TimerHandler pTimerHandler) {
										music.setCurrentTileIndex(index);
									}
								}));
					}
					audioEditor.commit();

					break;
				}
				return true;
			}
		};
		music.setCurrentTileIndex(index);
		sound = new TiledSprite(480, 10, 50, 40,
				resourcesManager.mTiledTextureRegionSound, vbom) {
			@Override
			public boolean onAreaTouched(
					final org.andengine.input.touch.TouchEvent pAreaTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				switch (pAreaTouchEvent.getAction()) {
				case org.andengine.input.touch.TouchEvent.ACTION_DOWN:
					break;
				case org.andengine.input.touch.TouchEvent.ACTION_UP:
					if (audioOptions.getBoolean("effectsOn", true)) {
						audioEditor.putBoolean("effectsOn", false);
						AlphaModifier mod = new AlphaModifier(0.3f, 1, 0);
						AlphaModifier mod1 = new AlphaModifier(0.0001f, 0, 1);
						this.registerEntityModifier(new SequenceEntityModifier(
								mod, mod1));
						indexS = 1;
						this.registerEntityModifier(mod);
						this.registerUpdateHandler(new TimerHandler(0.3f,
								new ITimerCallback() {

									@Override
									public void onTimePassed(
											TimerHandler pTimerHandler) {
										sound.setCurrentTileIndex(indexS);
									}
								}));
					} else {
						audioEditor.putBoolean("effectsOn", true);
						AlphaModifier mod = new AlphaModifier(0.3f, 1, 0);
						AlphaModifier mod1 = new AlphaModifier(0.0001f, 0, 1);
						this.registerEntityModifier(new SequenceEntityModifier(
								mod, mod1));
						indexS = 0;
						this.registerUpdateHandler(new TimerHandler(0.3f,
								new ITimerCallback() {

									@Override
									public void onTimePassed(
											TimerHandler pTimerHandler) {
										sound.setCurrentTileIndex(indexS);
									}
								}));
					}
					audioEditor.commit();

					break;
				}
				return true;
			}
		};
		sound.setCurrentTileIndex(indexS);
		// final Sprite x = new Sprite(rateW*520, -50,rateW*140,rateH*100,
		// resourcesManager.mTextureRegionX, vbom);
		// final Sprite x1 = new Sprite(rateW*520, -50,rateW*140,rateH*100,
		// resourcesManager.mTextureRegionX, vbom);
		// final Sprite x2 = new Sprite(rateW*520, -50,rateW*140,rateH*100,
		// resourcesManager.mTextureRegionX, vbom);
		x1 = new TiledSprite(590, 10, 35, 25,
				resourcesManager.mTiledTextureRegionX, vbom);
		x1.setCurrentTileIndex(0);
		x2 = new TiledSprite(620, 7, 50, 40,
				resourcesManager.mTiledTextureRegionX, vbom);
		x2.setCurrentTileIndex(0);
		x3 = new TiledSprite(658, 13, 65, 55,
				resourcesManager.mTiledTextureRegionX, vbom);
		x3.setCurrentTileIndex(0);

		this.attachChild(music);
		this.attachChild(sound);
		this.attachChild(x1);
		this.attachChild(x2);
		this.attachChild(x3);

		this.registerTouchArea(music);
		this.registerTouchArea(sound);
		this.setTouchAreaBindingOnActionDownEnabled(true);
		// add level
		// if(currentLevel==1)
		{
			Sprite level = new Sprite(0, -50, 170, 100,
					resourcesManager.mTextureRegionLevel, vbom);
			level.setRotation(12);
			level.registerEntityModifier(new MoveYModifier(1f, level.getY(), 0));
			this.attachChild(level);
		}
	}

	int index = 0;
	int indexS = 0;
	int x1x = 590, x1y = 10;
	int x2x = 620, x2y = 7;
	int x3x = 658, x3y = 13;

	TiledSprite sound;
	TiledSprite music;
	TiledSprite x1, x2, x3;

	public class MyCount extends CountDownTimer {

		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		public void onFinish() {

		}

		public void onTick(long millisUntilFinished) {
			Random r = new Random();
			int count = r.nextInt(5) + 2;
			for (int i = 0; i < count; i++) {
				addText();
			}
		}
	}

	private void createScore() {
		text = new Text(0, 0, resourcesManager.font, "0", 100, vbom);
		text.setColor(org.andengine.util.color.Color.BLUE);
		text.setPosition(165, 20);
		this.attachChild(text);
	}

	public Sprite backGroundSprite;

	private void createBackground() {
		backGroundSprite = new Sprite(0, 0, activity.CAMERA_WIDTH + 1,
				activity.CAMERA_HEIGHT + 1,
				resourcesManager.game_background_region, vbom);
		attachChild(backGroundSprite);
	}

	public void pauseGame() {
		this.setChildScene(mPauseScene, false, false, false);
		engine.stop();
	}

	public void unPauseGame() {
		this.clearChildScene();
		engine.start();
	}

	public class PauseableTimerHandler extends TimerHandler {

		private boolean mPause = false;

		public PauseableTimerHandler(float pTimerSeconds, boolean pAutoReset,
				ITimerCallback pTimerCallback) {
			super(pTimerSeconds, pAutoReset, pTimerCallback);
		}

		public void pause() {
			this.mPause = true;
		}

		public void resume() {
			this.mPause = false;
		}

		@Override
		public void onUpdate(float pSecondsElapsed) {
			if (!this.mPause) {
				super.onUpdate(pSecondsElapsed);
			}
		}
	}

	@Override
	public void onBackKeyPressed() {
		// isStopped = true;
		/*
		 * activity.runOnUiThread(new Runnable() {
		 * 
		 * @Override public void run() { engine.stop(); } });
		 */
		/*
		 * CameraScene mPauseScene = new CameraScene(activity.mCamera); Sprite
		 * pausedSprite = new Sprite(400, 200, resourcesManager.playgame_region,
		 * resourcesManager.vbom) {
		 * 
		 * @Override public boolean onAreaTouched(
		 * org.andengine.input.touch.TouchEvent pSceneTouchEvent, float
		 * pTouchAreaLocalX, float pTouchAreaLocalY) {
		 * getGameScene().clearChildScene(); engine.start(); Log.d("pos",
		 * "touch pause"); return super .onAreaTouched(pSceneTouchEvent,
		 * pTouchAreaLocalX, pTouchAreaLocalY); } };
		 * 
		 * mPauseScene.registerTouchArea(pausedSprite);
		 * mPauseScene.setTouchAreaBindingOnActionDownEnabled(true);
		 * mPauseScene.attachChild(pausedSprite); // makes the scene transparent
		 * mPauseScene.setBackgroundEnabled(false);
		 * getGameScene().attachChild(mPauseScene);
		 */
		/*
		 * Scene scene = new Scene(2); scene.setOnAreaTouchListener(new
		 * IOnAreaTouchListener() {
		 * 
		 * @Override public boolean onAreaTouched(
		 * org.andengine.input.touch.TouchEvent pSceneTouchEvent, ITouchArea
		 * pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		 * Log.d("pos", "pause"); return false; } }); CameraScene
		 * controlplayscene = new CameraScene(camera);
		 * controlplayscene.setOnAreaTouchListener(new IOnAreaTouchListener() {
		 * 
		 * @Override public boolean onAreaTouched(
		 * org.andengine.input.touch.TouchEvent pSceneTouchEvent, ITouchArea
		 * pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) {
		 * Log.d("pos", "pause1"); return false; } });
		 * controlplayscene.registerTouchArea(pausedSprite);
		 * scene.setChildScene(controlplayscene);
		 * getGameScene().setChildScene(scene);
		 */
		// createPauseGame();
		// PauseChildScene pau = new PauseChildScene();

		// this.registerTouchArea(pau.pausedSprite);
		// engine.stop();

		if (en % 2 == 0) {
			timer.mPause = true;
			timer1.mPause = true;
			timer2.mPause = true;
			timer3.mPause = true;
			timer4.mPause = true;
			timer5.mPause = true;
			timer6.mPause = true;
			timer7.mPause = true;
			timer8.mPause = true;
			timer9.mPause = true;
			// backGroundSprite.setAlpha(0.5f);
			getGameScene().setChildScene(mPauseScene, false, true, true);

			Log.d("pos", "pauseGame");
			// pauseGame();
			// engine.stop();
			// this.setChildScene(pauseScene(), false,true,true);
		} else {
			// engine.start();
			timer.mPause = false;
			timer1.mPause = false;
			timer2.mPause = false;
			timer3.mPause = false;
			timer4.mPause = false;
			timer5.mPause = false;
			timer6.mPause = false;
			timer7.mPause = false;
			timer8.mPause = false;
			timer9.mPause = false;
			// backGroundSprite.setAlpha(1.0f);
			getGameScene().clearChildScene();

			Log.d("pos", "unPauseGame");
			// unPauseGame();
		}
		en++;
		// SceneManager.getInstance().loadMenuScene(engine);

	}

	int en = 0;
	private final int MENU_PLAY = 0;
	private final int MENU_RESTART = 1;
	MenuScene menuChildScene;
	Sprite blur;

	public void createPauseGame() {
		/*
		 * blur = new Sprite(0,0, resourcesManager.game_background_region,
		 * vbom); blur.setAlpha(0.6f); attachChild(blur);
		 * 
		 * float
		 * playX=activity.CAMERA_WIDTH/2-resourcesManager.playgame_region.getWidth
		 * ()-10; float
		 * playY=activity.CAMERA_HEIGHT/2-resourcesManager.playgame_region
		 * .getHeight()/2; float restartX=activity.CAMERA_WIDTH/2+10; float
		 * restartY=playY; Log.d("restart","MENU_RESTART"+isPlay); Sprite play =
		 * new Sprite(playX,playY, resourcesManager.playgame_region, vbom){
		 * 
		 * @Override public boolean onAreaTouched( final
		 * org.andengine.input.touch.TouchEvent pAreaTouchEvent, final float
		 * pTouchAreaLocalX, final float pTouchAreaLocalY) { switch
		 * (pAreaTouchEvent.getAction()) { case
		 * org.andengine.input.touch.TouchEvent.ACTION_UP: if(!isPlay) {
		 * engine.start(); isPlay=true; } break; default: break; } return true;
		 * } }; play.setAlpha(0.6f); attachChild(play);
		 * this.registerTouchArea(play); final Sprite face = new Sprite(playX,
		 * playY, resourcesManager.playgame_region, vbom) {
		 * 
		 * @Override public boolean onAreaTouched(
		 * org.andengine.input.touch.TouchEvent pSceneTouchEvent, float
		 * pTouchAreaLocalX, float pTouchAreaLocalY) { // TODO Auto-generated
		 * method stub switch (pSceneTouchEvent.getAction()) { case
		 * org.andengine.input.touch.TouchEvent.ACTION_UP: if(!isPlay) {
		 * Log.d("restart","MENU_RESTART"+isPlay); engine.start(); isPlay=true;
		 * } Log.d("restart","MENU_RESTART"+isPlay); break; default: break; }
		 * return true;//super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
		 * pTouchAreaLocalY);
		 * 
		 * }
		 * 
		 * 
		 * }; attachChild(face); this.registerTouchArea(face);
		 * 
		 * Sprite restart = new Sprite(restartX,restartY,
		 * resourcesManager.restartgame_region, vbom){
		 * 
		 * @Override public boolean onAreaTouched( final
		 * org.andengine.input.touch.TouchEvent pAreaTouchEvent, final float
		 * pTouchAreaLocalX, final float pTouchAreaLocalY) { switch
		 * (pAreaTouchEvent.getAction()) { case
		 * org.andengine.input.touch.TouchEvent.ACTION_UP: if(!isPlay) {
		 * Log.d("restart","MENU_RESTART"+isPlay);
		 * SceneManager.getInstance().loadGameScene(engine); isPlay=true; }
		 * break; default: break; } return true; } }; restart.setAlpha(0.6f);
		 * attachChild(restart); this.registerTouchArea(restart);
		 * this.setTouchAreaBindingOnActionDownEnabled(true);
		 */
		// CameraScene cam = new CameraScene(camera);
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);

		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_PLAY, resourcesManager.playgame_region,
						vbom), 1.2f, 1);
		final IMenuItem restartMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_RESTART,
						resourcesManager.restartgame_region, vbom), 1.2f, 1);

		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(restartMenuItem);
		menuChildScene.setBackgroundEnabled(false);
		menuChildScene.buildAnimations();

		float playX = activity.CAMERA_WIDTH / 2
				- resourcesManager.playgame_region.getWidth() - 10;
		float playY = activity.CAMERA_HEIGHT / 2
				- resourcesManager.playgame_region.getHeight() / 2;
		float restartX = activity.CAMERA_WIDTH / 2 + 10;
		float restartY = playY;
		playMenuItem.setPosition(playX, playY);
		restartMenuItem.setPosition(restartX, restartY);

		menuChildScene.setOnMenuItemClickListener(this);

		setChildScene(menuChildScene);
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		// TODO Auto-generated method stub
		if (b) {
			super.onManagedUpdate(0);
		} else {
			super.onManagedUpdate(pSecondsElapsed);
		}
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		// TODO Auto-generated method stub
		switch (pMenuItem.getID()) {
		case MENU_PLAY:
			Log.d("pos", "MENU_PLAY" + isPlay);
			// getGameScene().clearChildScene();
			engine.start();
			break;
		case MENU_RESTART:
			if (!isPlay) {
				Log.d("pos", "MENU_RESTART");
				SceneManager.getInstance().loadGameScene(engine);
				isPlay = true;
			}

			break;

		}
		/*
		 * switch(arg1.getID()){ case 1: if(scene.hasChildScene()){
		 * scene.clearChildScene(); pauseBtn.setVisible(true); } return true;
		 * default: return false; }
		 */
		return false;
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_GAME;
	}

	@Override
	public void disposeScene() {
		camera.setHUD(null);
		camera.setChaseEntity(null); // TODO
		camera.setCenter(400, 240);

		// TODO code responsible for disposing scene
		// removing all game scene objects.

	}

	int xContinuos, k = 0;
	PhysicsWorld mPhysicsWorld;

	public void addText() {
		// resourcesManager.sliceWind.play();
		resourcesManager.playSoundWind();
		Log.d("addText", "addText");
		Random ran = new Random();
		int maxX = activity.CAMERA_WIDTH - 30;
		int minX = 30;
		int x1;
		if (isContinuos) {
			if (k == 0)
				xContinuos = x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
			else
				x1 = xContinuos;
			k++;
		} else
			x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
		final int x2;
		int x0 = 0;

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			x2 = x1 + ran.nextInt(activity.CAMERA_WIDTH / 4)
					+ activity.CAMERA_WIDTH / 4;
			x0 = (x2 - x1) / 2 + x1;
		} else {
			x1 -= 50;
			x2 = x1 - ran.nextInt(activity.CAMERA_WIDTH / 4)
					- activity.CAMERA_WIDTH / 4;
			x0 = (x1 - x2) / 2 + x2;
		}

		int y0 = ran.nextInt(activity.CAMERA_HEIGHT / 2 - 100) + 100;
		int y1;
		final int y2;
		y1 = activity.CAMERA_HEIGHT + 50;
		y2 = activity.CAMERA_HEIGHT + 50;
		Log.d("x1:y1", x1 + ":" + y1);
		Log.d("x0:y0", x0 + ":" + y0);
		Log.d("x2:y2", x2 + ":" + y2);
		float rangX = ran.nextFloat();
		float moveX = (rangX == 0) ? 0.0f : rangX + 2.5f;

		final Fruit fruit;
		fruit = FruitPool.sharedFruitPool().onAllocatePoolItem();
		fruit.sprit.setPosition(x1, y1);

		// target.setVisible(true);
		// target.detachSelf();
		fruit.sprit.setVisible(true);
		fruit.sprit.detachSelf();
		attachChild(fruit.sprit);
		{
			fruitList.add(fruit);
		}
		fruitCount++;
		Log.d("fruitCount", fruitCount + "");
		/*
		 * MoveModifier mod = new MoveModifier(3, fruit.sprit.getX(), 400,
		 * fruit.sprit.getY(), y0); MoveModifier mod1 = new MoveModifier(2, 400,
		 * 100, y0, y1+10);
		 * 
		 * fruit.sprit.registerEntityModifier(new
		 * SequenceEntityModifier(mod,mod1));
		 */
		if (x1 <= activity.CAMERA_WIDTH / 2) {

			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			// synchronized (mPhysicsWorld)
			registerUpdateHandler(mPhysicsWorld); // side=0
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 0, moveX); // left->right

		} else {
			// PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld); // si
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 1, moveX);// right->left

		}
		// unregisterUpdateHandler(mPhysicsWorld);
		// fruit.sprit.setVisible(true);
		// fruit.sprit.detachSelf()
		{

			// activity.mCurrentScene.getLastChild().attachChild(fruit.sprit);
			// synchronized (fruitList)

		}

	}

	public boolean sideTwo = true;

	public void addTwoSideText() {
		// resourcesManager.sliceWind.play();
		resourcesManager.playSoundWind();
		Random ran = new Random();
		int x1;
		if (sideTwo) {
			x1 = ran.nextInt(activity.CAMERA_WIDTH / 4 - 40) + 20;
			sideTwo = false;
		} else {
			x1 = ran.nextInt(activity.CAMERA_WIDTH / 4 - 40) + 500;
			sideTwo = true;
		}
		int y0 = ran.nextInt(activity.CAMERA_HEIGHT / 2 - 200) + 100;
		int y1;
		final int y2;
		y1 = activity.CAMERA_HEIGHT + 50;

		float rangX = ran.nextFloat();
		float moveX = 2.5f;

		final Fruit fruit;
		fruit = FruitPool.sharedFruitPool().onAllocatePoolItem();
		fruit.sprit.setPosition(x1, y1);

		fruit.sprit.setVisible(true);
		fruit.sprit.detachSelf();
		attachChild(fruit.sprit);
		{
			fruitList.add(fruit);
		}
		fruitCount++;
		Log.d("fruitCount", fruitCount + "");

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			// synchronized (mPhysicsWorld)
			registerUpdateHandler(mPhysicsWorld); // side=0
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 0, moveX); // left->right

		} else {
			PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld); // si
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 1, moveX);// right->left

		}

		// fruit.sprit.setVisible(true);
		// fruit.sprit.detachSelf()
		{

			// activity.mCurrentScene.getLastChild().attachChild(fruit.sprit);
			// synchronized (fruitList)

		}
	}

	public void addBoom() {
		// resourcesManager.sliceWind.play();
		resourcesManager.playSoundWind();
		Random ran = new Random();
		int maxHeight = activity.CAMERA_HEIGHT * 4 / 6;
		int minHeight = activity.CAMERA_HEIGHT / 6;
		int x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
		final int x2;
		int x0 = 0;

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			x2 = x1 + ran.nextInt(activity.CAMERA_WIDTH / 4)
					+ activity.CAMERA_WIDTH / 4;
			x0 = (x2 - x1) / 2 + x1;
		} else {
			x2 = x1 - ran.nextInt(activity.CAMERA_WIDTH / 4)
					- activity.CAMERA_WIDTH / 4;
			x0 = (x1 - x2) / 2 + x2;
		}
		int y0 = ran.nextInt(activity.CAMERA_HEIGHT / 2 - 100) + 100;
		int y1;
		final int y2;
		y1 = activity.CAMERA_HEIGHT + 50;
		y2 = activity.CAMERA_HEIGHT + 50;
		Log.d("x1:y1", x1 + ":" + y1);
		Log.d("x0:y0", x0 + ":" + y0);
		Log.d("x2:y2", x2 + ":" + y2);
		float moveX = ran.nextInt(4);
		final Boom boom;
		// = new Sprite(x, y, mTargetTextureRegion.deepCopy());
		boom = BoomPool.sharedBoomPool().onAllocatePoolItem();
		boom.sprit.setPosition(x1, y1);

		// target.setVisible(true);
		// target.detachSelf();
		boom.sprit.setVisible(true);
		boom.sprit.detachSelf();
		attachChild(boom.sprit);

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld);
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, boom.sprit, 0, moveX); // left->right
			// side=0
		} else {
			PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld);
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, boom.sprit, 1, moveX);// right->left
			// side=1
		}

		{

			synchronized (boomList) {
				boomList.add(boom);
			}

			boomCount++;
		}
		// activity.mHandler.postDelayed(mStartFruit, 1000);

	}

	public void setTextChange() {
		/*
		 * activity.runOnUpdateThread(new Runnable() {
		 * 
		 * @Override public void run() { // text.setPosition(160, 20);
		 * text.setText(score); } });
		 */
		text.setText(score);
		final float x = text.getX();
		final float y = text.getY();

		ScaleModifier sMod = new ScaleModifier(0.2f, 1f, 0.5f);
		ScaleModifier sMod1 = new ScaleModifier(0.2f, 0.5f, 1.6f);
		ScaleModifier sMod2 = new ScaleModifier(0.1f, 1.6f, 1f);
		float x1 = x;
		float y1 = y;
		MoveModifier mMod = new MoveModifier(0.1f, x1, x1 + 6, y1, y1 + 6);
		MoveModifier mMod1 = new MoveModifier(0.1f, text.getX(), x1 - 6,
				text.getY(), y1 + 6);
		MoveModifier mMod2 = new MoveModifier(0.1f, text.getX(), x1 + 6,
				text.getY(), y1 - 6);
		MoveModifier mMod3 = new MoveModifier(0.1f, text.getX(), x1 - 6,
				text.getY(), y1 - 6);

		// MoveModifier mMod4 = new MoveModifier(0.1f, mScene.text.getX(), y,
		// mScene.text.getY(), y);

		text.registerEntityModifier(new ParallelEntityModifier(
				new SequenceEntityModifier(mMod, mMod1, mMod2, mMod3),
				new SequenceEntityModifier(sMod, sMod1, sMod2)

		) {

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				// TODO Auto-generated method stub
				super.onModifierFinished(pModifier, pItem);
				text.setPosition(165, 20);
			}
		});
	}

	public void setTextChange(final Sprite sprite) {
		final float x = sprite.getX();
		final float y = sprite.getY();

		float x1 = x;
		float y1 = y;
		int sub = 10;
		MoveModifier mMod = new MoveModifier(0.2f, x1, x1 + 6, y1, y1 + 6);
		MoveModifier mMod1 = new MoveModifier(0.2f, sprite.getX(), x1 - 6,
				sprite.getY(), y1 + 6);
		MoveModifier mMod2 = new MoveModifier(0.2f, sprite.getX(), x1 + 6,
				sprite.getY(), y1 - 6);
		MoveModifier mMod3 = new MoveModifier(0.2f, sprite.getX(), x1 - 6,
				sprite.getY(), y1 - 6);
		MoveModifier mMod4 = new MoveModifier(0.2f, x1, x1 - sub, y1, y1 + sub);
		MoveModifier mMod5 = new MoveModifier(0.2f, sprite.getX(), x1 + sub,
				sprite.getY(), y1 - sub);
		MoveModifier mMod6 = new MoveModifier(0.2f, sprite.getX(), x1 + sub,
				sprite.getY(), y1 + sub);
		MoveModifier mMod7 = new MoveModifier(0.2f, sprite.getX(), x1 - sub,
				sprite.getY(), y1 - sub);

		// MoveModifier mMod4 = new MoveModifier(0.1f, mScene.text.getX(), y,
		// mScene.text.getY(), y);

		sprite.registerEntityModifier(new ParallelEntityModifier(mMod, mMod1,
				mMod2, mMod3, mMod4, mMod5, mMod6, mMod7) {

			@Override
			public void onModifierFinished(IModifier<IEntity> pModifier,
					IEntity pItem) {
				// TODO Auto-generated method stub
				super.onModifierFinished(pModifier, pItem);
				sprite.setPosition(0, 0);
			}
		});
	}

	public void detach() {
		clearUpdateHandlers();
		for (Object b : fruitList) {
			FruitPool.sharedFruitPool().onHandleRecycleItem((Fruit) b);
		}
		fruitList.clear();
		detachChildren();

	}

	public void registerUpdateByTimerHandler(com.example.manager.PauseableTimerHandler timer10) {
		this.engine.registerUpdateHandler(timer10);
	}

	public void unregisterUpdateByTimerHandler(
			com.example.manager.PauseableTimerHandler timer10) {
		this.engine.unregisterUpdateHandler(timer10);
	}

	public void vibrate() {
		//engine.vibrate(1000);
	}
	String textDiff;
	float durText;
	PauseableTimerHandler pau;
	ArrayList<PauseableTimerHandler> arr;
	ArrayList<PauseableTimerHandler> arrRun = new ArrayList<GameScene.PauseableTimerHandler>();
	
	public void showText(final String str) {
		Log.d("pos", "show text");
		arr = new ArrayList<GameScene.PauseableTimerHandler>();
		final Random ran = new Random();
		textDiff = "";
		for(int i=0;i<LevelManager.text.length();i++){
			String l = LevelManager.text.substring(i,i+1);
			int j;
			for(j=0;j<str.length();j++){
				String l2 = str.substring(j, j+1);
				if(l.equalsIgnoreCase(l2)){
					break;
				}
			}
			if(j==str.length()){
				textDiff += l;
			}
		}
		for (int i = 0; i < str.length(); i++) {
			final String letter = str.substring(i,i+1);
			PauseableTimerHandler timer1 = new PauseableTimerHandler(0.1f, false,
					new ITimerCallback() {
						@Override
						public void onTimePassed(TimerHandler pTimerHandler) {
								Log.d("pos", "timer1 is running");
								addText(letter);
								int k = ran.nextInt(textDiff.length());
								String l = textDiff.substring(k, k+1);
								addText(l);
								if(k<textDiff.length()/2){
									addBoom();					
								}
						}				
					});
			arr.add(timer1);			
		}
		//2 text
		for (int i = 0; i < str.length()/2; i++) {
			final String letter = str.substring(i,i+1);
			final String letter1 = str.substring(i+1,i+2);
			
			PauseableTimerHandler timer1 = new PauseableTimerHandler(0.1f, false,
					new ITimerCallback() {
						@Override
						public void onTimePassed(TimerHandler pTimerHandler) {
								Log.d("pos", "timer1 is running");
								addText(letter);
								addText(letter1);
								int k = ran.nextInt(textDiff.length());
								String l = textDiff.substring(k, k+1);
								addText(l);
								addBoom();													
						}				
					});
			arr.add(timer1);			
		}
		durText = 0.1f;
		final int n = arr.size();
		pau = new PauseableTimerHandler(durText, true, new ITimerCallback() {
					
					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						Log.d("pos", durText+",.");
						RingLeaderLevel.isTouchFinal = true;
						MultiTouchHandler.isTouchIgnore = true;
						if(arr.size()>0)
						{
						final int r = ran.nextInt(arr.size());
						//arr.get(r).setTimerSeconds(durText);
						//PauseableTimerHandler pau1 = arr.get(r);
						arrRun.add(arr.get(r));
						//pau.setTimerSeconds(durText);
						getGameScene().registerUpdateHandler(arrRun.get(arrRun.size()-1));
						durText += 1.3;
						arr.remove(r);
						pau.setTimerSeconds(durText);
						if(arr.size()==0){
							pau.setAutoReset(false);
							PauseableTimerHandler p = new PauseableTimerHandler(1f, false, new ITimerCallback() {
								
								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									okFinalLevel = true;								
								}
							});
							getGameScene().registerUpdateHandler(p);
							//getGameScene().unregisterUpdateHandler(pau);
						//	pauseFinalLevel();
							//RingLeaderLevel.getSharedInstance().goLevel2("fail");
						}
						}
					}
				});
		this.registerUpdateHandler(pau);
				/*for (int i = 0; i < n; i++) {
					activity.mHandler.postAtTime(new Runnable() {
						public void run() {
							final int r = ran.nextInt(arr.size());
							//arr.get(r).setTimerSeconds(durText);
							PauseableTimerHandler pau = arr.get(r);
							//pau.setTimerSeconds(durText);
							//getGameScene().registerUpdateHandler(pau);
							durText += 2000;
							arr.remove(r);
							Log.d("pos", durText+",.");
						}
					}, 100);
					
				}*/
			
		
		
		/*for (PauseableTimerHandler pauseableTimerHandler : arr) {
			this.registerUpdateHandler(pauseableTimerHandler);
		}*/
		/*count1 = 0; count2 = 0;
		timer1 = new PauseableTimerHandler(1, true,
				new ITimerCallback() {
					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
							Log.d("pos", "timer1 is running");
							addText("A");
							addText("B");
							addBoom();
							// delay1 += period1;
							timer1.setTimerSeconds(1);
							count1++;
							if (count1 == 1) {// 2
								// engine.unregisterUpdateHandler(timer1);
								// timer1.setTimerCallbackTriggered(true);
								timer1.setAutoReset(false);
							}
					}					
				});
		engine.registerUpdateHandler(timer1);
		timer2 = new PauseableTimerHandler(3.5f, true,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
							Log.d("pos", "timer2 is running");
							addText("A");
							addText("B");
							// delay1 += period1;
							timer2.setTimerSeconds(0.7f);
							count2++;
							if (count2 == 2) {// 4
								// engine.unregisterUpdateHandler(timer1);
								// timer1.setTimerCallbackTriggered(true);
								timer2.setAutoReset(false);
							}
					}					
				});
		engine.registerUpdateHandler(timer2);*/
				
	}
	private void addText(String string) {
		resourcesManager.playSoundWind();
		Log.d("addText", "addText");
		Random ran = new Random();
		int maxX = activity.CAMERA_WIDTH - 30;
		int minX = 30;
		int x1;
		if (isContinuos) {
			if (k == 0)
				xContinuos = x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
			else
				x1 = xContinuos;
			k++;
		} else
			x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
		final int x2;
		int x0 = 0;

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			x2 = x1 + ran.nextInt(activity.CAMERA_WIDTH / 4)
					+ activity.CAMERA_WIDTH / 4;
			x0 = (x2 - x1) / 2 + x1;
		} else {
			x1 -= 50;
			x2 = x1 - ran.nextInt(activity.CAMERA_WIDTH / 4)
					- activity.CAMERA_WIDTH / 4;
			x0 = (x1 - x2) / 2 + x2;
		}

		int y0 = ran.nextInt(activity.CAMERA_HEIGHT / 2 - 100) + 100;
		int y1;
		final int y2;
		y1 = activity.CAMERA_HEIGHT + 50;
		y2 = activity.CAMERA_HEIGHT + 50;
		Log.d("x1:y1", x1 + ":" + y1);
		Log.d("x0:y0", x0 + ":" + y0);
		Log.d("x2:y2", x2 + ":" + y2);
		float rangX = ran.nextFloat();
		float moveX = (rangX == 0) ? 0.0f : rangX + 2.5f;

		final Fruit fruit;
		fruit = FruitPool.sharedFruitPool().onAllocatePoolItem(string);
		fruit.sprit.setPosition(x1, y1);

		// target.setVisible(true);
		// target.detachSelf();
		fruit.sprit.setVisible(true);
		fruit.sprit.detachSelf();
		attachChild(fruit.sprit);
		{
			fruitList.add(fruit);
		}
		fruitCount++;
		Log.d("fruitCount", fruitCount + "");
		/*
		 * MoveModifier mod = new MoveModifier(3, fruit.sprit.getX(), 400,
		 * fruit.sprit.getY(), y0); MoveModifier mod1 = new MoveModifier(2, 400,
		 * 100, y0, y1+10);
		 * 
		 * fruit.sprit.registerEntityModifier(new
		 * SequenceEntityModifier(mod,mod1));
		 */
		if (x1 <= activity.CAMERA_WIDTH / 2) {

			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			// synchronized (mPhysicsWorld)
			registerUpdateHandler(mPhysicsWorld); // side=0
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 0, moveX); // left->right

		} else {
			// PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld); // si
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, fruit.sprit, 1, moveX);// right->left

		}
	}

	public void restartTimerHandler() {		
		timer = null;
		timer1 = null;
		timer2 = null;
		timer3 = null;
		timer4 = null;
		timer5 = null;
		timer6 = null;
		timer7 = null;
		timer8 = null;
		timer9 = null;
		engine.unregisterUpdateHandler(timer);
		engine.unregisterUpdateHandler(timer1);
		engine.unregisterUpdateHandler(timer2);
		engine.unregisterUpdateHandler(timer3);
		engine.unregisterUpdateHandler(timer4);
		engine.unregisterUpdateHandler(timer5);
		engine.unregisterUpdateHandler(timer6);
		engine.unregisterUpdateHandler(timer7);
		engine.unregisterUpdateHandler(timer8);
		engine.unregisterUpdateHandler(timer9);
		RunLevelById(currentLevel);
	}

	public void pauseFinalLevel() {
		Log.d("pos1", "pasue");
		pau.mPause = true;
		pau = null;
		for (PauseableTimerHandler p : arrRun) {
			p.mPause = true;
			p = null;
			this.unregisterUpdateHandler(p);
		}
		this.unregisterUpdateHandler(pau);
	}

	public void showGiftBox(AnimatedSprite giftBox1, AnimatedSprite giftBox2,
			AnimatedSprite giftBox3) {
		MultiTouchHandler.isGiftBox = true;
		addText(giftBox1);
	}

	private void addText(AnimatedSprite giftBox1) {
		resourcesManager.playSoundWind();
		Log.d("pos1", "addText");
		Random ran = new Random();
		int maxX = activity.CAMERA_WIDTH - 30;
		int minX = 30;
		int x1;
		if (isContinuos) {
			if (k == 0)
				xContinuos = x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
			else
				x1 = xContinuos;
			k++;
		} else
			x1 = ran.nextInt(activity.CAMERA_WIDTH - 60) + 20;
		final int x2;
		int x0 = 0;

		if (x1 <= activity.CAMERA_WIDTH / 2) {
			x2 = x1 + ran.nextInt(activity.CAMERA_WIDTH / 4)
					+ activity.CAMERA_WIDTH / 4;
			x0 = (x2 - x1) / 2 + x1;
		} else {
			x1 -= 50;
			x2 = x1 - ran.nextInt(activity.CAMERA_WIDTH / 4)
					- activity.CAMERA_WIDTH / 4;
			x0 = (x1 - x2) / 2 + x2;
		}

		int y0 = ran.nextInt(activity.CAMERA_HEIGHT / 2 - 100) + 100;
		int y1;
		final int y2;
		y1 = activity.CAMERA_HEIGHT + 50;
		y2 = activity.CAMERA_HEIGHT + 50;
		Log.d("x1:y1", x1 + ":" + y1);
		Log.d("x0:y0", x0 + ":" + y0);
		Log.d("x2:y2", x2 + ":" + y2);
		float rangX = ran.nextFloat();
		float moveX = (rangX == 0) ? 0.0f : rangX + 2.5f;

//		final Fruit fruit;
	//	fruit = FruitPool.sharedFruitPool().onAllocatePoolItem();
		giftBox1.setPosition(x1, y1);

		// target.setVisible(true);
		// target.detachSelf();
		giftBox1.setVisible(true);
		giftBox1.detachSelf();
		this.attachChild(giftBox1); 
		/*attachChild(fruit.sprit);
		{
			fruitList.add(fruit);
		}*/
		//fruitCount++;
		Log.d("fruitCount", fruitCount + "");
		/*
		 * MoveModifier mod = new MoveModifier(3, fruit.sprit.getX(), 400,
		 * fruit.sprit.getY(), y0); MoveModifier mod1 = new MoveModifier(2, 400,
		 * 100, y0, y1+10);
		 * 
		 * fruit.sprit.registerEntityModifier(new
		 * SequenceEntityModifier(mod,mod1));
		 */
		if (x1 <= activity.CAMERA_WIDTH / 2) {

			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			// synchronized (mPhysicsWorld)
			registerUpdateHandler(mPhysicsWorld); // side=0
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, giftBox1, 0, moveX); // left->right

		} else {
			// PhysicsWorld mPhysicsWorld;
			mPhysicsWorld = new PhysicsWorld(new Vector2(0,
					-SensorManager.GRAVITY_EARTH), false);
			registerUpdateHandler(mPhysicsWorld); // si
			IsRun i = new IsRun();
			i.isRunUp = true;
			addFace(x1, y1, y0, mPhysicsWorld, i, giftBox1, 1, moveX);// right->left

		}
	}

	public void modiferXOnOff(int posX2) {
		final ScaleModifier mod1 = new ScaleModifier(0.7f, 0.1f, 1.5f);
		final ScaleModifier mod2 = new ScaleModifier(0.4f, 1.5f, 1f);
		final AlphaModifier alpha = new AlphaModifier(1.1f, 0.1f, 1);
		switch (posX2) {
		case 1:
			x1.setCurrentTileIndex(0);
			x1.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
			x1.registerEntityModifier(alpha);
			break;
		case 2:
			x2.setCurrentTileIndex(0);
			x2.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
			x2.registerEntityModifier(alpha);
			break;
		case 3:
			x3.setCurrentTileIndex(0);
			x3.registerEntityModifier(new SequenceEntityModifier(mod1,mod2));
			x3.registerEntityModifier(alpha);
			break;

		default:
			break;
		}
		if(posX>0){
			posX--;
		}
	}

	public void addEffectSlashFruit(AnimatedSprite sprit) {
		AnimatedSprite left = new AnimatedSprite(sprit.getX()-50, sprit.getY(), resourcesManager.rLeft, resourcesManager.vbom);
		AnimatedSprite right = new AnimatedSprite(sprit.getX()+50, sprit.getY(), resourcesManager.rRight, resourcesManager.vbom);
		left.animate(100);
		right.animate(100);
		
		this.attachChild(right);
		this.attachChild(left);
		
	}
}
